﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.BLL.Extend;
namespace ZX.Web.UI.Controls
{
    public class YuiTreeListPage : ListPage
    {
        /// <summary>
        /// 需要取得部门信息的实体
        /// </summary>
        public List<string> lstNeedDeptInfoEntityName = new List<string>() {
            "V_hrEmployee", "V_hrEmployeeDeparture", "V_wkWorkResult","V_wkProjectWorkResult","V_wkConfirmWorkResult",
            "V_wkEmpWeekWorkResult","V_wkEmpMonthWorkResult","V_wkEmpYearWorkResult","V_xzEmpInsurance","V_xzEmpInsuranceDeptTotal",
            "V_wkScheduling","V_wkProjectWorkResult","V_xzEmpGJJ","V_xzEmpGJJDeptTotal","V_salEmployeeSalaryResult","V_ExaminationObject",
            "V_ExaminationRole","V_ExaminationResult","V_ExaminationResultTotal","V_wkOvertimeLeaveDeal"
        };

        /// <summary>
        /// 是否需要取得部门信息
        /// </summary>
        public bool IsGetDeptInfo
        {
            get
            {
                return (lstNeedDeptInfoEntityName.Contains(this.EntityName) || GetStringFromQueryString("IsDisplayChild", "") == "true");
            }
        }
        
        /// <summary>
        /// 获取数据数据条件
        /// </summary>
        public string Condition
        {
            get
            {
                return GetStringFromQueryString("condition", "");
            }
        }

        /// <summary>
        /// 定义实体ID
        /// </summary>
        public override string EntityId
        {
            get
            {
                BaseEntity instance = ZX.Common.Reflection.CreateEntitysInstance(this.EntityName) as BaseEntity;
                if (instance != null)
                {
                    return instance.GetTableId();
                }
                return "ListId";
            }
        }

        public string Order
        {
            get
            {
                return GetStringFromQueryString("order", "");
            }
        }

        /// <summary>
        /// LIST to LIST UIList主实体
        /// </summary>
        public string MasterEntityName
        {
            get
            {
                return GetStringFromQueryString("masterentity", "DatDepartment");
            }
        }

        /// <summary>
        /// Querystring formatter:?masterPara=SysFunction(tablename)|Name(Caption)|FunctionID(key)|ParentID(ParentKey)|FunctionPath
        /// 主信息参数
        /// </summary>
        public string MasterPara
        {
            get
            {
                return GetStringFromQueryString("masterPara", "");
            }
        }

        /// <summary>
        /// 明细实体名
        /// </summary>
        public string DetailEntityName
        {
            get;
            set;
        }

        //关联字段
        private string _foreignKey = "";
        public string ForeignKey
        {
            get
            {
                return _foreignKey;
            }
            set
            {
                _foreignKey = value;
            }

        }





        private void SetTreeControl(System.Web.UI.Control ctrl)
        {
            foreach (System.Web.UI.Control c in ctrl.Controls)
            {
                if (c is TreeView)
                {
                    var tv1 = c as TreeView;
                    if (!string.IsNullOrEmpty(MasterPara))
                    {


                        string[] paras = MasterPara.Split('|');
                        if (paras.Length > 4)
                        {
                            tv1.TableName = paras[0];
                            tv1.Caption = paras[1];
                            tv1.Key = paras[2];
                            tv1.ParentKey = paras[3];
                            tv1.path = paras[4];
                            tv1.Condition = getRoleCondition();
                            if (Request.QueryString["ForeignKey"] != null)
                                ForeignKey = Request.QueryString["ForeignKey"].ToString();
                        }

                        if (paras.Length > 5 && tv1 != null)
                        {
                            tv1.AllowCheck = bool.Parse(paras[5].Trim());
                        }



                    }
                     
                    if (GetStringFromQueryString("DeptType", "") != "")
                    {
                        if (GetStringFromQueryString("DeptType", "") != "")
                            tv1.IsOutDepartment =Int32.Parse(GetStringFromQueryString("DeptType", ""));
                    }
                    else
                    {
                        if (Session["IsOutSite"] != null)
                        {
                            tv1.IsOutDepartment = (bool)Session["IsOutSite"] == true ? 1 : 0;
                        }
                    }
                    tv1.userID = User.EmpID;
                }
                else
                {
                    if (Request.QueryString["ForeignKey"] != null)
                        ForeignKey = Request.QueryString["ForeignKey"].ToString();
                    SetTreeControl(c);
                }
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {

            base.OnLoad(e);
            SetTreeControl(this.Page);
        }


        /// <summary>
        /// 获取角色数据权限信息
        /// </summary>
        /// <returns></returns>
        public string getRoleCondition()
        {
            IList<EHR.Library.SysRoleFunction> list = (new ZX.BLL.BUser()).GetRoleFunction(this.UserId, this.FunctionID);
            StringBuilder strCondition = new StringBuilder("");

            if (list == null)
            {
                return "";
            }
            foreach (var entity in list)
            {
                switch (entity.OperateRangeType.Trim())
                {
                    ///只能查看本人数据
                    case "0":
                        if (strCondition.ToString() != "")
                        {
                            strCondition.Append(" OR ");
                        }
                        strCondition.Append(string.Format(" AddUser='{0}'", this.UserId));
                        break;
                    ///可以看本部门的
                    case "1":
                        if (this.EntityName == "V_CrmCustomer")
                        {
                            strCondition.Append(string.Format(" AreaId in  (select  VALUE  from  fn_Split((select DEPTIDS  from dbo.V_UserEmp  where userid='{0}'),',')) and IsPrevate='0'", this.UserId));
                        }
                        else
                        {
                            if (strCondition.ToString() != "")
                            {
                                strCondition.Append(" OR ");
                            }
                            strCondition.Append(string.Format(" AddUser in (select * from dbo.fn_GetUserDeptUsers('{0}'))", this.UserId));
                        }
                        break;
                    // 全部数据
                    case "2":
                        break;
                    // 自定义条件数据
                    case "3":
                        //strCondition.Append(string.Format(" AddUser in (select * from dbo.fn_GetUserDeptUsers('{0}'))", this.UserId));
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
            }

            //只用客户用户时使用
            if (this.User.IsOutsite == true)
            {
                if (this.EntityName == "TrActivity")
                {
                    if (strCondition.ToString() != "")
                    {
                        strCondition.Append(" OR ");
                    }

                    strCondition.Append(string.Format("(TrCustomerID='{0}')", User.CustomerID));

                }
                if (this.EntityName == "V_TrActivityCourse")
                {
                    if (strCondition.ToString() != "")
                    {
                        strCondition.Append(" OR ");
                    }

                    strCondition.Append(string.Format("(TrCustomerID = '{0}')", User.CustomerID));
                }


            }
            //if (this.EntityName == "V_TrStudyGain")
            //{
            //    if (strCondition.ToString() != "")
            //    {
            //        strCondition.Append(" OR ");
            //    }

            //    strCondition.Append(string.Format("(PrincipalHrmanno = '{0}')", this.UserId));
            //}

            return string.IsNullOrEmpty(strCondition.ToString()) ? "" : "(" + strCondition.ToString() + ")";
            //this.UserId
        }



    }
}
